<?php
	require_once('lib/nusoap.php');

	//ten dang nhap tren web 
	$username = 'vegafone';
	//mat khau dang nhap tren web
	$password = '123456';
	//secrec key duoc admin cung cap
	$secrec_key = '7718E1E9FA9B04603B6C13FD19E5FBCF6661D999';
	//brandname se goi tin nhan
	$brandname = 'VEGAFONE';
	//so dien thoai nhan tin nhan
	//VIETTEL
	#0982407803
	#0972565620
	#01694459599
	#0978437948
	#01656176473
	//MOBI
	#0939598926
	//VINA
	#0919250405

	$phonenumber = '0919250405';
	//noi dung tin nhan
	$sms_content = "Chuc mung ban da kich hoat thanh cong, ma qua tang cua ban la 0123456789. Hay den ngay dai ly VEGAFONE gan nhat de su dung mon qua den tu VEGAFONE nhe.";
	//duong dan webservice goi tin nhan
	$endpoint='http://210.245.42.37/BrandnameConnector/services/BrandnameConnector?wsdl';
	//secure pass duoc tao ra tu mat khau va secrec key
	$secure_pass = strtoupper(md5($secrec_key + $password));
	
	//file chua sessionId
	$myFile = "sessionid.txt";
	$file_content = '';	
	
	//kiem tra file session neu khong ton tai thi tao moi
	if(!file_exists($myFile))
	{
		echo 'file chua ton tai';
		$fh = fopen($myFile, 'w') or die("can't open file");
	}
	else//neu file da ton tai thi doc noi dung de lay ra sessionId va thoi gian de kiem tra xem sessionId con hieu luc khong
	{
		echo 'file ton tai<br>';
		//mo file o che do chi doc
		$fh = fopen($myFile, 'r') or die("can't open file");
		//lay noi dung 1 dong
		$file_content = fgets($fh);		
		//cat noi dung dua theo dau =
		$tmp_content = explode("=", $file_content);
		//phan 1 la sessionId
		$sessionId = $tmp_content[0];
		//phan 2 la thoi gian su dung lan cuoi cung cua sessionId
		$time = $tmp_content[1];
		//convert chuoi thoi gian trong file thanh kieu date
		$date_in_file = date('d-m-Y H:i:s', strtotime($time));
		//dong file
		fclose($fh);
	}
	
	$client = new nusoap_client($endpoint);
	$cur_date = date('d-m-Y H:i:s');//lay thoi gian hien tai		
	$minute = date_diff_2($date_in_file, $cur_date);//kiem tra thoi gian hien tai va thoi gian trong file
	
	//neu file khong co noi dung, hoac thoi gian trong file so voi hien tai da qua 9 phut thi thuc hien goi ham login lai de lay sessionId moi va thoi gian moi
	if($file_content == '' || $minute['minutes_total'] > 9)
	{
		echo 'goi ham login<br>';
		//tao doi tuong LoginRequest
		$paramLogin = array('username'=>$username, 'password'=>$password, 'secure_pass'=>$secure_pass);
		//goi ham login de lay sessionId
		$loginResult = $client->call("login",array('login'=>$paramLogin),'http://services');
		
		//ket qua tra ve cua ham login
		$rsCode = $loginResult['result'];//ma loi
		$desc = $loginResult['description'];//mo ta
		$sessionId = $loginResult['sessionid'];//sessionId

		echo 'Result code: '.$rsCode.'<br>';
		echo 'Description: '.$desc.'<br>';
		echo 'Sessionid: '.$sessionId.'<br>';
		//ghi sessionId moi va thoi gian hien tai vao file
		$fh = fopen($myFile, 'w');
		fwrite($fh, $sessionId.'='.date('d-m-Y H:i:s'));
		fclose($fh);
	}
	else
	{
		$rsCode = '0';
		echo 'sessionId con hieu luc<br>';
		//cap nhat lai thoi gian su dung cho sessionId trong file
		$fh = fopen($myFile, 'w');
		fwrite($fh, $sessionId.'='.date('d-m-Y H:i:s'));
		fclose($fh);
	}
	
	if($rsCode == '0')
	{
		//sau khi da kiem tra sessionId thi tien hanh goi webservice goi tin nhan
		echo 'Call send sms<br>';
		$paramSend = array('brandName'=>$brandname, 'receiver'=>$phonenumber, 'sessionid'=>$sessionId, 'smsContent'=>$sms_content, 'smsType'=>1);
		$result = $client->call("sendBrandSms",array('smsRequest'=>$paramSend),'http://services');
		print_r($result);
	}
	
	
	//ham so sanh 2 thoi gian.
	function date_diff_2($d1, $d2){
		$d1 = (is_string($d1) ? strtotime($d1) : $d1);
		$d2 = (is_string($d2) ? strtotime($d2) : $d2);

		$diff_secs = abs($d1 - $d2);
		$base_year = min(date("Y", $d1), date("Y", $d2));

		$diff = mktime(0, 0, $diff_secs, 1, 1, $base_year);
		return array(
			"years" => date("Y", $diff) - $base_year,
			"months_total" => (date("Y", $diff) - $base_year) * 12 + date("n", $diff) - 1,
			"months" => date("n", $diff) - 1,
			"days_total" => floor($diff_secs / (3600 * 24)),
			"days" => date("j", $diff) - 1,
			"hours_total" => floor($diff_secs / 3600),
			"hours" => date("G", $diff),
			"minutes_total" => floor($diff_secs / 60),
			"minutes" => (int) date("i", $diff),
			"seconds_total" => $diff_secs,
			"seconds" => (int) date("s", $diff)
		);
	}
?>